<template>
  <div>
    <el-button type="primary" @click="openAddDialog">添加客机</el-button>

    <el-table :data="airliners" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="50"></el-table-column>
      <el-table-column prop="kjname" label="客机名称" width="150"></el-table-column>
      <el-table-column prop="remark" label="备注" width="300"></el-table-column>
      <el-table-column prop="buyDate" label="购买日期" width="180"></el-table-column>
      <el-table-column prop="useDate" label="使用日期" width="180"></el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="mini" @click="openEditDialog(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteAirliner(scope.row.id)">删除</el-button>
        </template>

      </el-table-column>
    </el-table>

    <!-- 编辑客机对话框 -->
    <el-dialog title="编辑客机" v-model="isEditDialogVisible">
      <el-form :model="editForm">
        <el-form-item label="客机名称">
          <el-input v-model="editForm.kjname"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.remark"></el-input>
        </el-form-item>
        <el-form-item label="购买日期">
          <el-date-picker v-model="editForm.buyDate" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="使用日期">
          <el-date-picker v-model="editForm.useDate" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <!-- eslint-disable-next-line -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="isEditDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateAirliner">确认</el-button>
      </div>
    </el-dialog>

    <!-- 添加客机对话框 -->
    <el-dialog title="添加客机" v-model="isAddDialogVisible">
      <el-form :model="addForm">
        <el-form-item label="客机名称">
          <el-input v-model="addForm.kjname"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addForm.remark"></el-input>
        </el-form-item>
        <el-form-item label="购买日期">
          <el-date-picker v-model="addForm.buyDate" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="使用日期">
          <el-date-picker v-model="addForm.useDate" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <!-- eslint-disable-next-line -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="isAddDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addAirliner">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { listAirliners, deleteAirliner as deleteAirlinerAPI, updateAirliner as updateAirlinerAPI, addAirliner as addAirlinerAPI } from '@/api/airliner.js';
import { ElMessageBox, ElMessage } from 'element-plus';
import moment from 'moment';

const airliners = ref([]);
const isEditDialogVisible = ref(false);
const isAddDialogVisible = ref(false);

const editForm = ref({
  id: '',
  kjname: '',
  remark: '',
  buyDate: '',
  useDate: ''
});

const addForm = ref({
  kjname: '',
  remark: '',
  buyDate: '',
  useDate: ''
});
const addAirliner = async () => {
  try {
    const newAirliner = {
      ...addForm.value,
      buyDate: formatDateTime(addForm.value.buyDate),
      useDate: formatDateTime(addForm.value.useDate)
    };
    await addAirlinerAPI(newAirliner);
    ElMessage({
      type: 'success',
      message: '添加客机成功!'
    });
    isAddDialogVisible.value = false;
    fetchAirliners();
  } catch (error) {
    ElMessage({
      type: 'error',
      message: '添加客机失败'
    });
    console.error('Error adding airliner:', error);
  }
};

const fetchAirliners = async () => {
  try {
    const response = await listAirliners();
    if (response.code === 1) {
      airliners.value = response.data;
    } else {
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('Error fetching airliners:', error);
  }
};
const formatDateTime = (dateTime) => {
  return moment(dateTime).format('YYYY-MM-DD HH:mm:ss');
};

const openEditDialog = (airliner) => {
  editForm.value = { ...airliner };
  isEditDialogVisible.value = true;
};

const openAddDialog = () => {
  addForm.value = {
    kjname: '',
    remark: '',
    buyDate: '',
    useDate: ''
  };
  isAddDialogVisible.value = true;
};

const updateAirliner = async () => {
  try {
    const updatedAirliner = {
      ...editForm.value,
      buyDate: formatDateTime(editForm.value.buyDate),
      useDate: formatDateTime(editForm.value.useDate)
    };
    await updateAirlinerAPI(updatedAirliner);
    ElMessage({
      type: 'success',
      message: '更新客机成功!'
    });
    isEditDialogVisible.value = false;
    fetchAirliners();
  } catch (error) {
    ElMessage({
      type: 'error',
      message: '更新客机失败'
    });
    console.error('Error updating airliner:', error);
  }
};





const deleteAirliner = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除该客机吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });
    await deleteAirlinerAPI(id);
    ElMessage({
      type: 'success',
      message: '删除成功!'
    });
    fetchAirliners();
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage({
        type: 'error',
        message: '删除客机失败'
      });
    }
  }
};

onMounted(() => {
  fetchAirliners();
});
</script>

<style scoped>
/* 添加你的样式 */
</style>
